home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / Sample 2.4 Think C distribution / fullpath.c < prev    next >
C/C++ Source or Header  |  1990-07-09  |  3KB  |  129 lines

  1. /* Mac file system parameters */
  2. #define MAXPATH 256    /* Max path name length+1 */
  3. #define SEP ':'        /* Separator in path names */
  4. #define ROOTID 2    /* DirID of a volume's root directory */
  5. #define NULL 0
  6.  
  7. #include <string.h>
  8.  
  9. /* Macro to find out whether we can do HFS-only calls: */
  10. #define FSFCBLen (* (short *) 0x3f6)
  11. #define hfsrunning() (FSFCBLen > 0)
  12.  
  13. static char *
  14. getdirname(short dir)    /* dir = WDrefNum */
  15. {
  16.     union {
  17.         HFileInfo f;
  18.         DirInfo d;
  19.         WDPBRec w;
  20.         VolumeParam v;
  21.     } pb;
  22.     static char cwd[2*MAXPATH];
  23.     char namebuf[MAXPATH];
  24.     short err;
  25.     long dirid= 0;
  26.     char *next= cwd + sizeof cwd - 1;
  27.     int len;
  28.     
  29.     if (!hfsrunning())
  30.         return "";
  31.     
  32.     for (;;) {
  33.         pb.d.ioNamePtr= (StringPtr)namebuf;
  34.         pb.d.ioVRefNum= dir;
  35.         pb.d.ioFDirIndex= -1;
  36.         pb.d.ioDrDirID= dirid;
  37.         err= PBGetCatInfo((CInfoPBPtr)&pb.d, FALSE);
  38.         if (err != noErr) {
  39. /*            dprintf("PBCatInfo err %d", err);    */
  40.             return NULL;
  41.         }
  42.         *--next= SEP;
  43.         len= namebuf[0];
  44.         /* There is no overflow check on cwd here! */
  45.         strncpy(next -= len, namebuf+1, len);
  46.         if (pb.d.ioDrDirID == ROOTID)
  47.             break;
  48.         dirid= pb.d.ioDrParID;
  49.     }
  50.     return next;
  51. }
  52.  
  53.  
  54. static void
  55. fullpath(char *buf, short wdrefnum, char *file)
  56. {
  57.     strcpy(buf, getdirname(wdrefnum));
  58.     strcat(buf, file);
  59. }
  60.  
  61.  
  62.  
  63. /*
  64.  
  65. Subject: Re: Full path name of a file
  66. Date: 6 May 88 23:34:14 GMT
  67.  
  68. >Given the information in a SFReply, how do I construct the full path name
  69. >of a file (as a string), i e "disk:dir1:dir2:...:filename" ?
  70.  
  71. Be certain that you understand the difference between "working directory
  72. reference numbers", which SFReply returns, and directory ID numbers, which
  73. you will use to find the path name. (I only had to read the file manager
  74. chapter in Inside Mac Volume IV about ten times before understanding this!).
  75.  
  76. Basically, you want to know the "real" volume (NOT the working directory
  77. reference number) and the "real" directory ID, which are two different things,
  78. neither of which being the working directory reference number. While you can
  79. take the "working directory reference number" from the SFReply structure and
  80. convert it into both volume and directory ID numbers, it is easier to take
  81. advantage of two global variables, CurDirStore and SFSaveDisk (see insert
  82. on page 72 of Inside Macintosh Vol IV). After any of the SF routines, these
  83. global variables are set as follows, REGARDLESS OF WHETHER THE USER CHOSE
  84. A FILE OR CANCELLED:
  85.  
  86.   CurDirStore = directoryID (long int)
  87.   SFSaveDisk = 0 - volume (int)
  88.  
  89. Thus, if the user changed directories but cancelled the operation, the above
  90. variables are set the way the user left them (to the new directory). It is
  91. convenient to keep track of these separately. The following code from MacJove
  92. shows a simple way to recover the path using these. In this case, the
  93. current directory is stored in  cur_dir and current volume in cur_vol, which
  94. are set elsewhere (cur_dir - CurDirStore; cur_vol = 0 - SFSaveDisk.
  95.  
  96. */
  97.  
  98. static char *getwd(void)
  99. {
  100.     DirInfo d;
  101.     static char ret[255];
  102.     char nm[50], tmp[255];
  103.     
  104.     ret[0] = '\0';
  105.     d.ioDrDirID = CurDirStore;
  106.     for(;;) {
  107.         d.ioCompletion = 0;
  108.         d.ioNamePtr = (StringPtr) nm;
  109.         d.ioVRefNum = - SFSaveDisk;
  110.         d.ioFDirIndex = -1;
  111.  
  112.         PBGetCatInfo((CInfoPBPtr)&d,0);
  113.         if(d.ioResult != noErr) return(0);
  114.         PtoCstr((char *) nm);
  115.         strcpy(tmp,ret);
  116.         strcpy(ret,":");
  117.         strcat(ret,nm);
  118.         strcat(ret,tmp);
  119.         if(d.ioDrDirID == 2) break;    /* home directory */
  120.         d.ioDrDirID = d.ioDrParID;
  121.     } 
  122.     return(ret);
  123. }
  124.  
  125. void main(void)
  126. {
  127.     char *foo = getwd();
  128.     }
  129.